package tk.mybatis.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import tk.mybatis.springboot.model.TActivity;
import tk.mybatis.springboot.model.TJoiners;
import tk.mybatis.springboot.model.TUser;
import tk.mybatis.springboot.service.ActivityJoinerService;
import tk.mybatis.springboot.service.ActivityService;
import tk.mybatis.springboot.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by NightDream on 2017/9/22.
 */
@RestController
@RequestMapping("/mobile")
public class MobileController extends BaseController {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityJoinerService activityJoinerService;
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/joinpage")
    public ModelAndView joinPage() {
        ModelAndView modelAndView = new ModelAndView("mobile/joinpage");
        return modelAndView;
    }


    public TActivity getCurrentActivity() {
        TActivity activity = new TActivity();
        activity.setIscurrent(0);
        TActivity tActivity = activityService.getTActivity(activity);
        if (tActivity != null) {
            log.info("活动获取成功");
        } else {
            log.info("活动获取失败");
        }
        return tActivity;
    }

    /**
     * 用户参与活动
     *
     * @param joiner
     */
    @RequestMapping(value = "/joinActivity")
    public JSONObject joinActivity(TJoiners joiner, HttpServletRequest request) throws Exception {
        log.info("{} 参与了活动", joiner.getRealname());
        JSONObject jsonObject = new JSONObject();
        if (TextUtils.isEmpty(joiner.getRealname())) {
            return getFaildResponse(jsonObject, "用户名为空！");
        }
        TActivity activity = getCurrentActivity();
        if (activity == null) {
            return getFaildResponse(jsonObject, "活动尚未创建，无法参与！");
        }
        if (activity.getStatus().equals("0")) {
            return getFaildResponse(jsonObject, "活动尚未开始，无法参与！");
        }
        if (activity.getStatus().equals("2")||activity.getStatus().equals("3")) {
            return getFaildResponse(jsonObject, "活动已截止，感谢您的参与！");
        }
        joiner.setActivityId(activity.getId());
        joiner.setCreateTime(new Date());
        joiner.setSex(joiner.getSex());
        if (joiner.getSex() == 0) {
            joiner.setHeadimg("user1-128x128.jpg");
        } else {
            joiner.setHeadimg("user3-128x128.jpg");
        }
        TUser tUser = new TUser();
        tUser.setCreateDate(new Date());
        tUser.setLoginIp(request.getRemoteAddr());
        tUser.setName(joiner.getRealname());
        tUser.setSex(joiner.getSex());
        userService.save(tUser);
        activityJoinerService.save(joiner);
        return getSuccessResponse(jsonObject, "活动参加成功！");
    }

}
